package cn.jianyongjie.system.controller;

import cn.jianyongjie.system.service.IMenuService;
import cn.jianyongjie.system.domain.Menu;
import cn.jianyongjie.system.query.MenuQuery;
import cn.jianyongjie.basic.utils.AjaxResult;
import cn.jianyongjie.basic.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    public IMenuService menuService;

    /*
    * @Title: MenuController
    * @Description: 无线极树
    * @Author: JianYongJie
    * @Version: 1.0
    * @Date:  2022/5/15 13:04
    * @Parameters: * @param
    * @Return java.util.List<cn.jianyongjie.system.domain.Menu>
    */
    @GetMapping("/findTree")
    public List<Menu> findTree() {
        return menuService.findTree();
    }

    /**
     * 保存和修改公用的
     * @param menu  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Menu menu){
        try {
            if( menu.getId()!=null)
                menuService.update(menu);
            else
                menuService.save(menu);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            menuService.remove(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Menu get(@PathVariable("id")Long id)
    {
        return menuService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Menu> list(){

        return menuService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return QueryResult 分页对象
    */
    @PostMapping("/list")
    public QueryResult<Menu> json(@RequestBody MenuQuery query)
    {
        return menuService.queryData(query);
    }
}
